﻿// <copyright file="RetryTest.cs" company="N/A">
//     Copyright (c) Nicholas Barrett and Benjamin Kasten.  All rights reserved.
//
//     This program is free software; you can redistribute it and/or modify it
//     under the terms of the GNU General Public License as published by the
//     Free Software Foundation; either version 2 of the License, or (at your
//     option) any later version.
//
//     This program is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
//     Public License for more details.
//
//     You should have received a copy of the GNU General Public License along
//     with this program; if not, write to the Free Software Foundation, 
//     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// </copyright>

namespace BetterAzureTableStorage.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.WindowsAzure.StorageClient;

    /// <summary>
    /// Contains tests for the Retry class
    /// </summary>
    [TestClass]
    public class RetryTest
    {
        /// <summary>
        /// Test that TRetryException constructs without exception
        /// </summary>
        [TestMethod]
        public void ConstructorTests()
        {
            TRetryException exception;

            exception = new TRetryException();
            
            string message = "This is a test.";
            exception = new TRetryException(message);
            Assert.AreEqual<string>(message, exception.Message);
        }

        /// <summary>
        /// Test that InvokeWithRetry fails all retry attempts propertly.
        /// </summary>
        [TestMethod]
        public void InvokeWithRetryTest_Failure()
        {
            const int Retries = 5;

            int retryCount = 0;
            bool excepted = false;
            TTableStorageException lastException = null;

            try
            {
                Retry.Invoke(
                    () =>
                    {
                        retryCount++;
                        lastException = new TTableStorageException();
                        throw lastException;
                    }, RetryPolicies.Retry(Retries, TimeSpan.FromSeconds(0)));
            }
            catch (TRetryException ex)
            {
                Assert.AreEqual<int>(Retries, retryCount);
                Assert.AreEqual<TTableStorageException>(lastException, ex.InnerException as TTableStorageException);

                excepted = true;
            }

            Assert.IsTrue(excepted);
        }

        /// <summary>
        /// Test for InvokeWithRetry
        /// </summary>
        [TestMethod]
        public void InvokeWithRetryTest()
        {
            const int Retries = 5;

            int retryCount = 0;

            Retry.Invoke(
                () =>
                {
                    retryCount++;

                    if (retryCount != Retries - 1)
                    {
                        throw new TTableStorageException();
                    }
                }, RetryPolicies.Retry(Retries, TimeSpan.FromSeconds(0)));

            Assert.AreEqual<int>(Retries - 1, retryCount);
        }
    }
}
